/**
 * 链接远程服务器
  * @param ip 服务器ip
 */

void call(String ip ) {
     withCredentials([sshUserPrivateKey(
             credentialsId: 'ssh_key',  // 凭证 ID
             keyFileVariable: 'SSH_KEY',
             usernameVariable: 'USER'
     )]) {
          sh(script: """
                    # 1. 创建 Jenkins 工作目录的 .ssh 文件夹（如果不存在）
                        mkdir -p /var/jenkins_home/.ssh && chmod 700 /var/jenkins_home/.ssh
                        
                        # 2. 将远程服务器的主机密钥添加到 known_hosts
                        ssh-keyscan ${ip} >> /var/jenkins_home/.ssh/known_hosts
                        chmod 600 /var/jenkins_home/.ssh/known_hosts  # 限制权限，确保 SSH 信任该文件
                        # 使用 Jenkins 提供的临时密钥文件路径
                        ssh   -o UserKnownHostsFile=/var/jenkins_home/.ssh/known_hosts -i \$SSH_KEY \$USER@${ip} 
                    """)
     }


}
/**
 * 有返回值的远程脚本
 * 需要在withCredentials方法内执行
 * @param ip
 * @param shell
 * @return
 */
def sshReturn(String ip ,String shell) {
       def res= sh(script: """
                        # 使用 Jenkins 提供的临时密钥文件路径
                        ssh   -o UserKnownHostsFile=/var/jenkins_home/.ssh/known_hosts -i \$SSH_KEY \$USER@${ip}  "${shell}"
                    """,returnStdout: true)
        return res
}
/**
 * 无返回值的远程脚本
 * 需要在withCredentials方法内执行
 * @param ip
 * @param shell
 */
void sshVoid(String ip ,String shell) {
         sh(script: """
                        # 使用 Jenkins 提供的临时密钥文件路径
                        ssh   -o UserKnownHostsFile=/var/jenkins_home/.ssh/known_hosts -i \$SSH_KEY \$USER@${ip}  "${shell}"
                    """)

}

/**
 * 无返回值的远程脚本
 * 需要在withCredentials方法内执行
 * @param ip
 * @param shell
 */
void sshTomcatVoid(String ip ,String javaHome,String shell) {
    def command = buildCommand(javaHome)

    String fullCommand = "ssh -o UserKnownHostsFile=/var/jenkins_home/.ssh/known_hosts -i \$SSH_KEY \$USER@${ip}  \"${command} ${shell}\""

    sh(script: fullCommand)
}
@NonCPS
def buildCommand(String javaHome) {
    return " export JAVA_HOME=${javaHome} && export PATH=${javaHome}/bin:\$PATH &&  "}
/**
 * 无返回值的远程脚本
 * @param ip
 * @param shell
 */
void loginSshVoid(String ip ,String shell) {
    withCredentials([sshUserPrivateKey(
            credentialsId: 'ssh_key',  // 凭证 ID
            keyFileVariable: 'SSH_KEY',
            usernameVariable: 'USER'
    )]) {
        sh(script: """
                        # 使用 Jenkins 提供的临时密钥文件路径
                        ssh   -o UserKnownHostsFile=/var/jenkins_home/.ssh/known_hosts -i \$SSH_KEY "\$USER"@${ip}  "${shell}"
                    """)
    }
}

/**
 * 有返回值的远程脚本
 * 需要在withCredentials方法内执行
 * @param ip
 * @param shell
 * @return
 */
def loginSshReturn(String ip,String shell) {
    withCredentials([sshUserPrivateKey(
            credentialsId: 'ssh_key',  // 凭证 ID
            keyFileVariable: 'SSH_KEY',
            usernameVariable: 'USER'
    )]) {
        def res = sh(script: """
                        # 使用 Jenkins 提供的临时密钥文件路径
                        ssh   -o UserKnownHostsFile=/var/jenkins_home/.ssh/known_hosts -i \$SSH_KEY "\$USER"@${ip}  "${shell}"
                    """, returnStdout: true)
        return res

    }



}


def getPid(String ip , String projectName) {
    String pid = sshReturn(ip, "  ps -ef | grep ${projectName} | grep -v grep | grep -v ssh | awk '{print \\\$2}'")

    if (util.strIsBlack(pid)) {
        return ""
    }
    String pids = " ";
    pid.split("\n").each {
        pids = pids + it + " "
    }
    return pids
}